import { UserService } from './../user/user.service';
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Article } from './article.entity';
import { PaginateResult } from 'src/interfaces/http.interface';
import { ArticleComment } from './article.comment.entity';
import * as marked from 'marked'

@Injectable()
export class ArticleService {

    constructor(
        @InjectRepository(Article)
        private readonly articleRepository: Repository<Article>,
        @InjectRepository(ArticleComment)
        private readonly articleCommentRepository: Repository<ArticleComment>,
        private readonly userService: UserService,
    ) { }

    public async get(id: number): Promise<Article> {
        const data = await this.articleRepository.findOne(
            id,
        )
        if (!data) {
            throw new HttpException("ID错误", HttpStatus.BAD_REQUEST)
        }
        data.content = marked(data.content)
        data.authorInfo = this.userService.get()
        return data
    }

    public async getNullable(id: number): Promise<Article | null> {
        const data = await this.articleRepository.findOne(
            id,
        )
        if (!data) {
            return null
        }
        data.content = marked(data.content)
        data.authorInfo = this.userService.get()
        return data
    }

    public async list(pageIndex = 1, pageSize = 10): Promise<PaginateResult<Article>> {
        const [list, count] = await this.articleRepository.findAndCount({
            skip: pageSize * (pageIndex - 1),
            take: pageSize,
        })
        const authorInfo = this.userService.get()
        list.forEach(item => {
            item.content = marked(item.content)
            item.authorInfo = authorInfo
        })
        return {
            list,
            count,
            pageIndex,
            pageSize,
            pageCount: Math.ceil(count / pageSize)
        }
    }

    public async edit(
        id: number,
        article: Pick<Article, 'title' | 'author' | 'pic' | 'content' | 'keywords'>,
    ): Promise<Article> {
        await this.articleRepository.update({
            id,
        }, {
            title: article.title,
            author: article.author,
            content: article.content,
        })
        return await this.get(id)
    }

    public async add(
        article: Pick<Article, 'title' | 'author' | 'pic' | 'content' | 'keywords'>
    ): Promise<Article> {
        if (!article.keywords) {
            article.keywords = []
        }
        const data = await this.articleRepository.save(article)
        return data
    }

    public async del(id: number) {
        await this.articleRepository.delete(id)
    }

    public async getCommentList(articleId: number, pageIndex = 1, pageSize = 10) {
        const [list, count] = await this.articleCommentRepository.findAndCount({
            skip: pageSize * (pageIndex - 1),
            take: pageSize,
            where: {
                articleId
            },
            order: {
                id: 'DESC'
            }
        })
        return {
            list,
            count,
            pageIndex,
            pageSize,
            pageCount: Math.ceil(count / pageSize)
        }
    }

    public async addComment(
        articleId: number,
        comment: Pick<ArticleComment, 'name' | 'email' | 'website' | 'content'>
    ): Promise<ArticleComment> {
        return await this.articleCommentRepository.save(
            {
                ...comment,
                articleId
            }
        )
    }

}
